package com.payment.web.controller.app.shopping;

import com.payment.common.base.AjaxResult;
import com.payment.common.type.OrderStatus;
import com.payment.common.utils.DateUtils;
import com.payment.common.utils.StringUtils;
import com.payment.system.domain.ShoppingOrder;
import com.payment.system.domain.ShoppingOrderShipment;
import com.payment.system.service.IShoppingOrderService;
import com.payment.system.service.IShoppingOrderShipmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fuzhili
 * @Description: 商城订单处理
 * @date 2019/1/22 16:46
 */
@Controller
@RequestMapping("/app/shoppingOrder")
public class OrderController {
    private static final Logger log = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private IShoppingOrderService shoppingOrderService;
    @Autowired
    private IShoppingOrderShipmentService shoppingOrderShipmentService;


    /**
     * app端 提交订单(立即支付) 生成订单
     *
     * @param merchantId
     * @param productId
     * @param buyNumber
     * @return
     */
    @PostMapping("/submit")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult submitOrder(String merchantId, String productId, Integer buyNumber, String addressId) {
        try {
            return shoppingOrderService.submitOrder(merchantId, productId, buyNumber, addressId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("app提交订单失败="+e.toString());
            return AjaxResult.error("提交订单失败!请联系客服咨询");
        }
    }




    /**
     * 订单列表
     *
     * @param merchantId
     * @param orderStatus
     * @return
     * @see com.payment.common.type.OrderStatus
     */
    @RequestMapping("/list")
    @ResponseBody
    public AjaxResult cartSubmitOrder(String merchantId, String orderStatus, Integer pageNum, Integer pageSize) {
        return shoppingOrderService.selectShoppingOrderListByMerchantId(merchantId, orderStatus, pageNum, pageSize);
    }

    /**
     * 订单详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/orderDetails")
    @ResponseBody
    public AjaxResult orderDetails(String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            return AjaxResult.error(1, "获取订单详情失败：缺失参数");
        }
        ShoppingOrder order = shoppingOrderService.selectShoppingOrderById(orderId);
        //判断是否为未支付 显示订单超时时间
        String overtimeMsg = "";
        if (StringUtils.equals(order.getOrderStatus(), OrderStatus.NO_PAY.getCode())) {
            overtimeMsg = DateUtils.getDatePoorByMin(DateUtils.getNowDate(), order.getCreateTime());
            if (overtimeMsg.length() == 7) {//已超时修改订单状态为 已取消
                shoppingOrderService.updateOrderStatusByOrederId(order.getId());
            }
        }
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("overtime", overtimeMsg);
        map.put("order", shoppingOrderService.selectShoppingOrderById(orderId));
        ShoppingOrderShipment orderShipment = shoppingOrderShipmentService.selectShoppingOrderShipmentByOrderId(orderId);

        if (orderShipment==null) {
            ShoppingOrderShipment orderShipment1=new ShoppingOrderShipment();
            orderShipment1.setUserPhone("400-808-3305");
            orderShipment1.setProvinceName("河北省");
            orderShipment1.setCityName("石家庄市");
            orderShipment1.setUserAdress("江西大厦4层4090");
            orderShipment1.setDistrictName("新华区");
            orderShipment1.setUserName("5G智能时代");
            map.put("address", orderShipment1);
        }else{
            map.put("address", orderShipment);
        }


        return AjaxResult.successAndData("获取订单详情成功", map);
    }


    /**
     * 取消订单
     *
     * @param orderId
     * @return
     */
    @PostMapping("/cancelOrder")
    @ResponseBody
    public AjaxResult cancelOrder(String orderId) {
        return shoppingOrderService.updateOrderStatusByOrederId(orderId);
    }

    /**
     * 删除订单
     *
     * @param orderId
     * @return
     */
    @PostMapping("/delOrder")
    @ResponseBody
    public AjaxResult delOrder(String orderId) {
        return shoppingOrderService.updateOrderDelStatusByOrederId(orderId);
    }

    /**
     * 确认订单（确认收获）
     *
     * @param orderId
     * @return
     */
    @PostMapping("/confirmOrder")
    @ResponseBody
    public AjaxResult confirmOrder(String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            return AjaxResult.error(1, "缺失参数");
        }
        ShoppingOrder order = shoppingOrderService.selectShoppingOrderById(orderId);
        if (order == null) {
            return AjaxResult.error(1, "该订单不存在");
        }
        order.setOrderStatus(OrderStatus.SHIPPED.getCode());
        int r = shoppingOrderService.updateShoppingOrder(order);
        return r > 0 ? AjaxResult.success() : AjaxResult.error();
    }





}
